<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/1/11 0011
 * Time: 17:15
 */

namespace app\index\model;


use think\facade\Request;
use think\Model;

class GoodsProp extends Model
{

    protected $pk = 'pid';
    // 修改器
    public function setPvaluesAttr($value,$data)
    {
        return implode(',',$value);
    }
    //搜索器
    public function searchPnameAttr($query,$value)
    {
        if(!empty($value)){
            $query->whereLike('pname',"%$value%");
        }
    }

    public function searchPvaluesAttr($query,$value)
    {
        if(is_array($value) && count($value)>0){
            $value = implode($value);
            $query->whereFindInSet('pvalues',$value);
        }
    }
    //获取器
    public function getPvaluesZhAttr($value,$data)
    {
        $pv = GoodsPropValue::getGoodsPropValues();

        $pvArr = explode(',',$data['pvalues']);
        $result_str = '';
        foreach($pv as $k=>$v){
            if(in_array($k,$pvArr)){
                $result_str .= $v.',';
            }
        }
        $result_str = trim($result_str,',');
        return $result_str;
    }
    public static function getGoodsProp($pv=[],$param = [])
    {
        $field=['pid','pname','pvalues','status','sort_order'];
        //连接数据库
        $model = self::field($field)->withSearch(array_keys($param),$param)->order('sort_order','desc');
        $paginate=$model->paginate(['list_rows'=>10,'query'=>$param])->toArray();
//        echo "<pre>";
//        print_r($model->paginate(['list_rows'=>2,'query'=>$param])->toArray());
//        echo "</pre>";
        //toArray吐饿瑞  toaway吐饿位
        $page=Request::get('page',1);
        if($page>$paginate['last_page']){
            $page=$paginate['last_page'];
        }
        //数据修改
        $arr_Tmp = [];
        $result = $model->paginate(['list_rows'=>10,'page'=>$page,'query'=>$param])->each(function($item) use($pv,$arr_Tmp,$param){

            $arr_Tmp = explode(',',$item['pvalues']);
            $item['pvalues_zn'] = '';
            foreach($pv as $k=>$v){
                foreach ($arr_Tmp as $tv){
                    if($tv == $k){//循环构建中文新字段
                        $item['pvalues_zn'] .= $v.',';
                    }
                }
            }
            $item['pvalues_zn'] = trim($item['pvalues_zn'],',');
            //搜索内容 变红显示
            if(!empty($param['pname'])){
                $item['pname'] = str_replace($param['pname'],
                    '<span style="color: red;font-weight: 900">'.$param['pname'].'</span>',$item['pname']);
            }
            return $item;

        });

        //返回数据
        return $result;
    }

    public function propAdd(array $data, array $allowField = [], bool $replace = false)
    {
        try{
            if(empty($data)){
                $this->error = '新增数据不能为空！';
                return ;
            }
            // 限制写入的字段列表
            if (!empty($allowField))
                $this->allowField($allowField);
            $this->replace($replace)->save($data);

            return true;
        }catch (Exception $ex){
            $this->error = $ex->getMessage();
            return false;
        }
    }
    public function getInsertId(){// 获取id的方法
        $data = $this->toArray();
        return $data[$this->pk];
    }
    // 修改
    public function changetryone($data, $where = [], $allowField = [])
    {
        try{
            if (empty($data)){
                $this->error = "修改数据不能为空!!";
                return;
            }
            if (!empty($allowField)){
                $this->allowField($allowField);

            }
            if (!empty($where)){
                $this->setUpdateWhere($where);
                if (isset($data[$this->pk]))  unset($data[$this->pk]);
            }
            return $this->exists(true)->save($data);

        }catch (\Exception $e){
            $this->error = $e->getMessage();
            return false;
        }
    }
}